package com.winchain.chainbill.bill.biz.repurchase.service.impl;

import com.winchain.chainbill.ChainBillUtil;
import com.winchain.chainbill.asset.biz.credit.rx.BillAssetTranSubscriber;
import com.winchain.chainbill.basic.context.TimeoutOperation;
import com.winchain.chainbill.bill.base.po.CbOpt;
import com.winchain.chainbill.bill.base.po.CbRpur;
import com.winchain.chainbill.bill.base.service.CbRpurService;
import com.winchain.chainbill.bill.biz.repurchase.rx.BillRpurPmtSubscriber;
import com.winchain.chainbill.bill.biz.repurchase.rx.BillRpurRejectSubscriber;
import com.winchain.chainbill.bill.biz.repurchase.service.BillRpurPmtService;
import com.winchain.chainbill.bill.biz.repurchase.vo.BillRpur;
import com.winchain.chainbill.bill.biz.send.rx.BatchBillEvent;
import com.winchain.chainbill.bill.biz.send.vo.BatchBillStats;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import rx.Observable;

@Service("billRpurPmtService")
public class BillRpurPmtServiceImpl implements BillRpurPmtService {
	@Autowired
	private CbRpurService cbRpurService;
	@Autowired
	private BillRpurPmtSubscriber billRpurPmtSubscriber;
	@Autowired
	private BillRpurRejectSubscriber billRpurRejectSubscriber;
	@Autowired
	private BillAssetTranSubscriber billAssetTranSubscriber;

	public BatchBillStats<CbRpur> pmt(String... billIds) {
		List<BillRpur> bills = this.cbRpurService.selectBillRpursByBillIdAndState("1", billIds);

		BatchBillEvent<BillRpur, CbRpur> event = new BatchBillEvent();
		event.setRecs(bills);
		CbOpt opt = new CbOpt();
		opt.setCurState("S014");
		opt.setOptCode("E002");
		opt.setRemark("����������");

		TimeoutOperation timeoutOpt = new TimeoutOperation();
		timeoutOpt.setOptCode("E004");
		timeoutOpt.setRemark("��������������,������������");

		BatchBillStats<CbRpur> stats = new BatchBillStats();

		event.setOperation(opt);
		event.setTimeoutOpt(timeoutOpt);
		event.setStats(stats);
		event.setValue("assetEventType", "cb_hld_pmt");

		Observable<BatchBillEvent<BillRpur, CbRpur>> observers = Observable.just(event);
		observers.subscribe(this.billRpurPmtSubscriber);
		observers.subscribe(this.billAssetTranSubscriber);
		if (ChainBillUtil.onChain()) {
			Object txSnHost = event.getValue("txSnHost");
			if (txSnHost != null) {
				stats.setTxSnHost((String) txSnHost);
			}
		}
		if ((bills == null) || (bills.size() == 0)) {
			stats.addAbnormalMsg("������������������������");
		} else if (billIds.length > bills.size()) {
			stats.addAbnormalMsg("����������������������������");
		}
		return stats;
	}

	public BatchBillStats<CbRpur> rpReject(String remark, String... billIds) {
		List<BillRpur> bills = this.cbRpurService.selectBillRpursByBillIdAndState("1", billIds);

		BatchBillEvent<BillRpur, CbRpur> event = new BatchBillEvent();
		event.setRecs(bills);
		CbOpt opt = new CbOpt();
		opt.setCurState("S014");
		opt.setOptCode("E003");
		opt.setRemark(remark);

		TimeoutOperation timeoutOpt = new TimeoutOperation();
		timeoutOpt.setOptCode("E004");
		timeoutOpt.setRemark("��������,������������");

		BatchBillStats<CbRpur> stats = new BatchBillStats();
		event.setStats(stats);
		event.setOperation(opt);
		event.setTimeoutOpt(timeoutOpt);
		Observable<BatchBillEvent<BillRpur, CbRpur>> observers = Observable.just(event);

		observers.subscribe(this.billRpurRejectSubscriber);
		if ((bills == null) || (bills.size() == 0)) {
			stats.addAbnormalMsg("������������������������");
		} else if (billIds.length > bills.size()) {
			stats.addAbnormalMsg("����������������������������");
		}
		return stats;
	}
}
